package com.yidu.hbatsams.action.emp;

import com.yidu.hbatsams.domain.AtsBaseSport;
import com.yidu.hbatsams.domain.AtsEmpAthlete;
import com.yidu.hbatsams.domain.AtsEmpAthleteResults;
import com.yidu.hbatsams.domain.vo.UserVo;
import com.yidu.hbatsams.service.AtsBaseSportService;
import com.yidu.hbatsams.service.emp.AtsEmpAthleteResultsService;
import com.yidu.hbatsams.service.emp.AtsEmpAthleteService;
import com.yidu.hbatsams.utils.Message;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 比赛成绩表的控制层
 */
@Controller
@RequestMapping("/UserseControllerAthleteResults")
public class AtsEmpAthleteResultsController {
    @Resource
    AtsEmpAthleteResultsService atsEmpAthleteResultsService;
    @Resource
    AtsBaseSportService atsBaseSportService;
    @Resource
    AtsEmpAthleteService atsEmpAthleteService;
    /**
     * 查询所有+模糊查询
     * @param pageNum
     * @param pageSize
     * @param matchName
     * @return
     */
    @ResponseBody
    @RequestMapping("selectAll")
    Map<String,Object> findAll(@RequestParam("page") Integer pageNum, @RequestParam("limit") Integer pageSize, String matchName,String startTime, String endTime, HttpSession session){
        UserVo user = (UserVo) session.getAttribute("user");
        Map<String,Object> map = new HashMap<>();
        Long count;
        List<AtsEmpAthleteResults> list = new ArrayList<>();
        if("admin".equals(user.getUserName())){
            count = atsEmpAthleteResultsService.count();
            list =atsEmpAthleteResultsService.selectAll(pageNum,pageSize,matchName,startTime,endTime);
        }else{
            count = atsEmpAthleteResultsService.coutCoachResults(Integer.valueOf(user.getClerkId()));
            list = atsEmpAthleteResultsService.selectAllCoach(pageNum,pageSize,matchName,startTime,endTime,Integer.valueOf(user.getClerkId()));
        }
        map.put("code", 0);
        map.put("msg", "");
        map.put("count", count); // 总行数
        map.put("data", list);
        return map;
    }

    /**
     * 逻辑删除
     * @param resultId
     * @return
     */
    @ResponseBody
    @RequestMapping("deleteByPrimaryKey")
    public Message deleteById(Integer resultId){
        Message msg = new Message();
        atsEmpAthleteResultsService.deleteByPrimaryKey(resultId);
        msg.setNum(1);
        msg.setMsg("删除成功！");
        return msg;
    }
    /**
     * 批量删除
     * @param resultIds
     * @return
     */
    @ResponseBody
    @RequestMapping("deleteMany")
    public Message deleteMoreById(String resultIds){
        String[] ids = null;
        Message msg = new Message();
        msg.setNum(1);
        //如果为批量删除
        if(resultIds!=null && !"".equals(resultIds)){
            ids=resultIds.split(",");
        }
        for (int i = 0; i < ids.length; i++){
            String resultId = ids[i];
            atsEmpAthleteResultsService.deleteByPrimaryKey(Integer.valueOf(resultId));
        }
        msg.setMsg("操作成功");
        return msg;
    }
    /**
     * 查询项目名称
     * @return
     */
    @ResponseBody
    @RequestMapping("findBaseSportName")
    public List<AtsBaseSport> findBySportName(){
        return atsBaseSportService.findAllSportName();
    }
    /**
     * 查询运动员名称
     * @return
     */
    @ResponseBody
    @RequestMapping("findByAthleteName")
    public List<AtsEmpAthlete> findByAthleteName(HttpSession session){
        UserVo user = (UserVo) session.getAttribute("user");
        List<AtsEmpAthlete> list = new ArrayList<>();
        if("admin".equals(user.getUserName())){
            list = atsEmpAthleteService.findByAthleteId();
        }else{
            list = atsEmpAthleteService.findAllByCoachId(Integer.valueOf(user.getClerkId()));
        }
        return list;
    }

    /**
     * 根据ID查询
     * @param resultId
     * @return
     */
    @ResponseBody
    @RequestMapping("selectByPrimaryKey")
    public AtsEmpAthleteResults findById(Integer resultId){
        return atsEmpAthleteResultsService.selectByPrimaryKey(resultId);
    }
    /**
     * 新增加修改
     * @param record
     * @return
     */
    @ResponseBody
    @RequestMapping("insertAlter")
    public Message addUpdate(AtsEmpAthleteResults record,HttpSession session){
        Message mes = new Message();
        UserVo userTwo = (UserVo) session.getAttribute("user");
        //根据运动员名称查ID
        int athleteName = Integer.valueOf(record.getAthleteName());
        AtsEmpAthlete athlete = atsEmpAthleteService.findById(athleteName);
        record.setAthleteName(athlete.getAthleteName());
        record.setAthleteId(athleteName);

        if(record.getResultId() != null && record.getResultId() > 0){
            record.setCreater(userTwo.getUserName());//设置创建人
            atsEmpAthleteResultsService.updateByPrimaryKey(record);
        }else if (record.getResultId() == null){
            record.setCreater(userTwo.getUserName());//设置创建人
            atsEmpAthleteResultsService.insert(record);
            mes.setNum(1);
        }

        mes.setMsg("操作成功！");
        return mes;
    }
}
